//
//  HXTQJRXMViewController.m
//  KD
//
//  Created by GaoFei on 16/6/27.
//  Copyright © 2016年 Whx. All rights reserved.
//

#define xmAreaTFTag 100
#define xmNameTFTag 101
#define xmAddressTFTag 102
#define jsUnitsTag 103
#define jsPrincipalTag 104
#define jsTelePhoneTag 105

#define AlertFiveStr @"暂无资质等级"
#define AlertFourStr @"暂无企业"
#define AlertThreeStr @"暂无人员类型"
#define AlertTwoStr @"请先选择企业类型"
#define AlertOneStr @"请先选择其他筛选信息"

#define kShangChuanShuJuUrl @"Intervention/IntProject.ashx"

#import "HXTQJRXMViewController.h"

@interface HXTQJRXMViewController ()<UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource>{

    NSArray * _textFieldArr;//所有的textFiled
}

/**项目区域*/
@property (weak, nonatomic) IBOutlet UITextField *xmAreaTextField;

/**项目名称*/
@property (strong, nonatomic) IBOutlet UITextField *xmNameTextField;

/**项目地址*/
@property (weak, nonatomic) IBOutlet UITextField *xmAddressTextField;

/**建设单位*/
@property (weak, nonatomic) IBOutlet UITextField *jsUnitsTextField;

/**建设单位负责人*/
@property (weak, nonatomic) IBOutlet UITextField *jsPrincipalTextField;

/**建设单位电话*/
@property (weak, nonatomic) IBOutlet UITextField *jsTelePhoneTextField;



//-------pickerView
/**地区的pickerView*/
@property(nonatomic,strong) UIPickerView *areaPickerView;//tag AreaViewTag

//------ 数据源数组
/**网络数据-地区*/
@property(nonatomic,strong) NSMutableArray <HXGetAreaCodeTestModelList *> *areaCodeTestArray;


//----- UIToolbar
@property(nonatomic,strong) UIToolbar  *keyboardDoneButtonBar;

//---- 地区的 areacode
@property (nonatomic,copy) NSString * areacode;

@end

@implementation HXTQJRXMViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"提前介入项目";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self getAreaAndCorpKindData];//获取地区列表绑定滚轮或下拉列表
    
    //设置名称
    _xmAddressTextField.text = _addressStr;
    
    [self loadViewLayout];
}

-(void)getAreaAndCorpKindData{
    
    //获取地区列表绑定滚轮或下拉列表
    [NetworkAPI getAreaCodeTestSuccess:^(HXGetAreaCodeTestModel *result){
        
        if ([result.code isEqualToString:@"200"]) {
            
            [self.areaCodeTestArray removeAllObjects];
            [self.areaCodeTestArray addObjectsFromArray:result.resultlist];
            [self.areaPickerView reloadAllComponents];
            
        }else{
            [SVProgressHUD showErrorWithStatus:@"获取地区列表为空"];
        }
    } failure:^(NSError *err) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
}

/**设置页面布局*/
-(void)loadViewLayout{

    //textField的数组
    _textFieldArr = @[_xmAreaTextField,
                               _xmNameTextField,
                               _xmAddressTextField,
                               _jsUnitsTextField,
                               _jsPrincipalTextField,
                               _jsTelePhoneTextField];
    
    for (UITextField * textField in _textFieldArr) {
        
        textField.delegate = self;
        textField.layer.borderWidth = 0.5f;
        textField.layer.cornerRadius = 8.f;
        textField.font = [UIFont systemFontOfSize:18.f];
        textField.inputAccessoryView = self.keyboardDoneButtonBar;
        if (textField.tag == xmAreaTFTag) {
            
            textField.inputView = self.areaPickerView;
        }
    }
}


#pragma mark - UIPickerViewDelegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    if (pickerView.tag == xmAreaTFTag) {
        return 1;
    }else{
    
        return 0;
    }
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    if (pickerView.tag == xmAreaTFTag) {
        
        return self.areaCodeTestArray.count;
    }
    
    return 0;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (pickerView.tag == xmAreaTFTag) {
        
        HXGetAreaCodeTestModelList * areaModel = self.areaCodeTestArray[row];
        
        return areaModel.area;
    }
    return nil;
}

//-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
////    if (pickerView.tag == xmAreaTFTag) {
////        HXGetAreaCodeTestModelList *areas = self.areaCodeTestArray[row];
////        self.xmAreaTextField.text = areas.area;
////        _areacode = areas.areacode;
////    
////    }
//}

#pragma mark - TextFieldDelegate
-(void)textFieldDidEndEditing:(UITextField *)textField{
    HXLog(@"%@",textField.text);
    
    if (textField.tag == xmAreaTFTag) {
        
        NSInteger row = [self.areaPickerView selectedRowInComponent:0];
        HXGetAreaCodeTestModelList *areas = [self.areaCodeTestArray objectAtIndex:row];
        textField.text = areas.area;
        _areacode = areas.areacode;
        HXLog(@"--------%@--------",textField.text);
    }
    
    if ([GlobalUtil isNilValue:textField.text]) return;
    if ([textField.text isEqualToString:@"*"]) return;
//    if (textField.text.length >10) {
//        if ([[textField.text substringToIndex:(10)] isEqualToString:AlertOneStr]) return;
//    }
//    if (textField.text.length >8) {
//        if ([[textField.text substringToIndex:(8)] isEqualToString:AlertTwoStr]) return;
//    }
//    if (textField.text.length >6) {
//        if ([[textField.text substringToIndex:(6)] isEqualToString:AlertThreeStr]) return;
//    }
//    if (textField.text.length >4) {
//        if ([[textField.text substringToIndex:(4)] isEqualToString:AlertFourStr]) return;
//    }
//    if (textField.text.length >6) {
//        if ([[textField.text substringToIndex:(6)] isEqualToString:AlertFiveStr]) return;
//    }
    
   if (textField.tag == xmNameTFTag){
        HXLog(@"%@",textField.text);
    }else if (textField.tag == xmAddressTFTag){
        HXLog(@"%@",textField.text);
    }else if (textField.tag == jsUnitsTag){
        HXLog(@"%@",textField.text);
    }else if (textField.tag == jsPrincipalTag){
        HXLog(@"%@",textField.text);
    }else if (textField.tag == jsTelePhoneTag){
        HXLog(@"%@",textField.text);
    }
}

/**上传数据*/
-(void)loadShangChuanShuJu{

    // ---- 安全判断
    //项目区域
    NSString * areaStr = [self.xmAreaTextField text];
    //项目名称
    NSString * nameStr = [self.xmNameTextField text];
    //项目地址
    NSString * addressStr = [self.xmAddressTextField text];
    //建设单位
    NSString * unitsStr = [self.jsUnitsTextField text];
    //建设单位负责人
    NSString * principalStr = [self.jsPrincipalTextField text];
    //建设单位电话
    NSString * telePhoneStr = [self.jsTelePhoneTextField text];
    
    //判断
    if ([GlobalUtil isNilValue:areaStr]) {
        
        [self.xmAreaTextField becomeFirstResponder];
        
        return;
    }
    
    if ([GlobalUtil isNilValue:nameStr]) {
        
        [self.xmNameTextField becomeFirstResponder];
        
        return;
    }
    
    if ([GlobalUtil isNilValue:addressStr]) {
        
        [self.xmAddressTextField becomeFirstResponder];
        
        return;
    }
    
    if ([GlobalUtil isNilValue:unitsStr]) {
        
        [self.jsUnitsTextField becomeFirstResponder];
        
        return;
    }
    
    if ([GlobalUtil isNilValue:principalStr]) {
        
        [self.jsPrincipalTextField becomeFirstResponder];
        
        return;
    }
    
    if ([GlobalUtil isNilValue:telePhoneStr]) {
        
        [self.jsTelePhoneTextField becomeFirstResponder];
        
        return;
    }
    
    //添加提前介入项目
    [NetworkAPI addIntProjectWithPrjName:nameStr
                              PrjAddress:addressStr BuildCorpName:unitsStr Builder:principalStr BuilderTel:telePhoneStr   PointX:[NSString stringWithFormat:@"%0.6f",_coordinate.longitude]                                  PointY:[NSString stringWithFormat:@"%0.6f",_coordinate.latitude]                                 KindType:@"1"                                 AreaCode:_areacode KeyGuid:nil Success:^(HXBaseModel *result) {
                                      
                                      if ([result.code integerValue] == 200) {
                                          
                                         [SVProgressHUD showSuccessWithStatus:@"添加成功!"];
                                          
                                          [self.navigationController popViewControllerAnimated:YES];

                                      }else{
                                      
                                          [SVProgressHUD showErrorWithStatus:@"提交任务失败,请稍后再试!"];
                                      }
                                      
                                  } failure:^(NSError *err) {
                                      
                                    [SVProgressHUD showErrorWithStatus:@"提交任务失败!"];
                                      
                                  }];
}

/**提交按钮*/
- (IBAction)submitBtnAct:(id)sender {

    [self loadShangChuanShuJu];
}

/**------ 懒加载 -------**/
- (UIPickerView *)areaPickerView{
    if (_areaPickerView == nil) {
        _areaPickerView = [[UIPickerView alloc]init];
        _areaPickerView.dataSource = self;
        _areaPickerView.delegate = self;
        _areaPickerView.tag = xmAreaTFTag;
    }
    return _areaPickerView;
}

- (NSMutableArray<HXGetAreaCodeTestModelList *> *)areaCodeTestArray{
    if (_areaCodeTestArray == nil) {
        _areaCodeTestArray = [[NSMutableArray alloc]init];
    }
    return _areaCodeTestArray;
    
}

-(UIToolbar*)keyboardDoneButtonBar{
    if(!_keyboardDoneButtonBar){
        _keyboardDoneButtonBar= [[UIToolbar alloc] init];
        _keyboardDoneButtonBar.barStyle = UIBarStyleDefault;
        _keyboardDoneButtonBar.tintColor = nil;
        [_keyboardDoneButtonBar sizeToFit];
        UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                                  target:nil
                                                                                  action:nil];
//        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [cancelBtn addTarget:self action:@selector(CancelClicked:) forControlEvents:UIControlEventTouchUpInside];
//        cancelBtn.size = CGSizeMake(44, 44);
//        [cancelBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
//        UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc] initWithCustomView:cancelBtn];
        
        UIButton *okBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [okBtn addTarget:self action:@selector(DoneClicked:) forControlEvents:UIControlEventTouchUpInside];
        okBtn.size = CGSizeMake(44, 44);
        [okBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [okBtn setTitle:@"确定" forState:UIControlStateNormal];
        okBtn.titleLabel.textColor = [UIColor blueColor];
        UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithCustomView:okBtn];
        [_keyboardDoneButtonBar setItems:[NSArray arrayWithObjects:flexItem,doneButton, nil]];
        
    }
    return _keyboardDoneButtonBar;
}

//-(void)CancelClicked:(UIButton *)btn{
//    self.xmAreaTextField.text = @"";
//    self.xmNameTextField.text = @"";
//    self.xmAddressTextField.text = _addressStr;
//    self.jsUnitsTextField.text = @"";
//    self.jsPrincipalTextField.text = @"";
//    self.jsTelePhoneTextField.text = @"";
//    [self.view endEditing:YES];
//}

-(void)DoneClicked:(UIButton *)btn{
    
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
